/**
 * 
 */
package org.ranch.web.midlet;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.ranch.api.ConceptCategoryAnnotation;
import org.ranch.api.DefaultConceptCategories;
import org.ranch.api.service.ConceptService;
import org.ranch.api.utils.RanchUtil;
import org.ranch.model.Concept;
import org.ranch.model.ConceptCategory;
import org.springframework.stereotype.Component;

/**
 * @author Jmpango
 * 
 */
@Component("midletPrepareAnimal")
public class MidletPrepareAnimal implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private ConceptService conceptService;
    private String status;

    public MidletPrepareAnimal(String status, ConceptService conceptService) {
	setStatus(status);
	this.conceptService = conceptService;
    }

    private List<String> getAnimal() {
	List<String> animalsName = new ArrayList<String>();
	try {
	    ConceptCategoryAnnotation animalTypeCateoryAnnotation = RanchUtil
		    .getConceptCategoryFieldAnnotation(DefaultConceptCategories.class,
			    DefaultConceptCategories.ANIMAL_TYPE);

	    if (animalTypeCateoryAnnotation != null) {
		ConceptCategory concept = conceptService()
			.getConceptCategoryById(animalTypeCateoryAnnotation.id());
		if (concept != null) {
		    for (Concept c : conceptService().getConceptsByCategory(concept)) {
			animalsName.add(c.getName());
		    }
		}
	    }
	} catch (Exception e) {
	}
	return animalsName;
    }

    /**
     * @return
     */
    private List<String> getBreed() {
	List<String> breed = new ArrayList<String>();

	try {
	    ConceptCategoryAnnotation breedCateoryAnnotation = RanchUtil
		    .getConceptCategoryFieldAnnotation(DefaultConceptCategories.class,
			    DefaultConceptCategories.BREED_TYPE);

	    if (breedCateoryAnnotation != null) {
		ConceptCategory concept = conceptService()
			.getConceptCategoryById(breedCateoryAnnotation.id());
		if (concept != null) {
		    for (Concept c : conceptService().getConceptsByCategory(concept)) {
			breed.add(c.getName());
		    }
		}
	    }
	} catch (Exception e) {
	}

	return breed;
    }

    /**
     * @return
     */
    private List<String> getHealth() {
	List<String> health = new ArrayList<String>();
	try {
	    ConceptCategoryAnnotation healthStatusCateoryAnnotation = RanchUtil
		    .getConceptCategoryFieldAnnotation(DefaultConceptCategories.class,
			    DefaultConceptCategories.HEALTH_STATUS_TYPE);

	    if (healthStatusCateoryAnnotation != null) {
		ConceptCategory concept = conceptService()
			.getConceptCategoryById(healthStatusCateoryAnnotation.id());
		if (concept != null) {
		    for (Concept c : conceptService().getConceptsByCategory(concept)) {
			health.add(c.getName());
		    }
		}
	    }
	} catch (Exception e) {
	}

	return health;
    }

    /**
     * @return
     */
    private List<String> getColour() {
	List<String> color = new ArrayList<String>();
	try {
	    ConceptCategoryAnnotation colourCateoryAnnotation = RanchUtil
		    .getConceptCategoryFieldAnnotation(DefaultConceptCategories.class,
			    DefaultConceptCategories.COLOR_TYPE);

	    if (colourCateoryAnnotation != null) {
		ConceptCategory concept = conceptService
			.getConceptCategoryById(colourCateoryAnnotation.id());
		if (concept != null) {
		    for (Concept c : conceptService.getConceptsByCategory(concept)) {
			color.add(c.getName());
		    }
		}
	    }
	} catch (Exception e) {
	}
	return color;
    }
    
    public List<String> getAniStatus(){
	List<String> status = new ArrayList<String>();
	try {
	    ConceptCategoryAnnotation statusCateoryAnnotation = RanchUtil
		    .getConceptCategoryFieldAnnotation(DefaultConceptCategories.class,
			    DefaultConceptCategories.STATUS_TYPE);

	    if (statusCateoryAnnotation != null) {
		ConceptCategory concept = conceptService
			.getConceptCategoryById(statusCateoryAnnotation.id());
		if (concept != null) {
		    for (Concept c : conceptService.getConceptsByCategory(concept)) {
			status.add(c.getName());
		    }
		}
	    }
	} catch (Exception e) {
	}
	return status;
    }

    /**
     * @return
     * @throws IOException
     */
    public byte[] serializer() throws IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	DataOutputStream dos = new DataOutputStream(baos);

	String recordValue = "";

	dos.writeUTF(getStatus());
	dos.flush();

	for (String name : getColour()) {
	    recordValue += name + ":";
	}

	dos.writeUTF(recordValue);
	dos.flush();
	
	recordValue = "";
	for (String name : getHealth()) {
	    recordValue += name + ":";
	}

	dos.writeUTF(recordValue);
	dos.flush();

	recordValue = "";
	for (String name : getBreed()) {
	    recordValue += name + ":";
	}

	dos.writeUTF(recordValue);
	dos.flush();
	
	recordValue = "";
	for (String name : getAnimal()) {
	    recordValue += name + ":";
	}
	dos.writeUTF(recordValue);
	dos.flush();
	
	recordValue = "";
	for (String name : getAniStatus()) {
	    recordValue += name + ":";
	}
	dos.writeUTF(recordValue);
	dos.flush();
	
	recordValue = "";
	dos.close();
	baos.close();

	return baos.toByteArray();
    }

    /**
     * @return the conceptService
     */
    public ConceptService conceptService() {
	return conceptService;
    }

    /**
     * @param conceptService
     *            the conceptService to set
     */
    public void setConceptService(ConceptService conceptService) {
	this.conceptService = conceptService;
    }

    /**
     * @return the status
     */
    public String getStatus() {
	return status;
    }

    /**
     * @param status
     *            the status to set
     */
    public void setStatus(String status) {
	this.status = status;
    }

    /**
     * @return the serialversionuid
     */
    public static long getSerialversionuid() {
	return serialVersionUID;
    }

}
